package com.ljf.config.domain.validator;

import com.ljf.common.domain.validator.BasisValidator;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.utils.CollectionUtil;
import com.ljf.common.utils.EnumUtil;
import com.ljf.common.utils.StringUtil;
import com.ljf.config.domain.repository.ConfigBaseImpulseSenderHeaderRepository;
import com.ljf.config.domain.repository.ConfigDatImpulseSenderConditionRepository;
import com.ljf.config.dto.command.ConfigBaseImpulseSenderDetailCommand;
import com.ljf.config.dto.command.ConfigBaseImpulseSenderHeaderCommand;
import com.ljf.config.dto.command.ConfigDatImpulseSenderConditionCommand;
import com.ljf.config.dto.query.ConfigBaseImpulseSenderHeaderQuery;
import com.ljf.config.dto.query.ConfigDatImpulseSenderConditionQuery;
import com.ljf.config.enums.*;
import com.ljf.config.model.ConfigBaseImpulseSenderHeader;
import com.ljf.config.vo.ConfigBaseImpulseSenderHeaderVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 发号器配置表头  业务校验
 *
 * @author LJF
 * @since 2025-01-09
 */
@Component
@AllArgsConstructor
public class ConfigBaseImpulseSenderHeaderValidator extends BasisValidator<ConfigBaseImpulseSenderHeaderQuery, ConfigBaseImpulseSenderHeaderCommand, ConfigBaseImpulseSenderHeaderVO, ConfigBaseImpulseSenderHeader, ConfigBaseImpulseSenderHeaderRepository> {

    private final ConfigDatImpulseSenderConditionRepository conditionRepository;

    @Override
    public void checkAdd(ConfigBaseImpulseSenderHeaderCommand command) throws ServiceException {
        super.checkAdd(command);
        if (!EnumUtil.checkCode(command.getBelongServiceCode(), ConfigBaseImpulseSenderHeaderBelongServiceEnum.values())) {
            throw new ServiceException("新增校验失败:归属服务不存在");
        }
        if (CollectionUtil.isEmpty(command.getDetailList())) {
            throw new ServiceException("新增校验失败:配置明细不能为空");
        }
        if (!EnumUtil.checkCode(command.getRefreshMode(), ConfigBaseImpulseSenderHeaderRefreshModeEnum.values())) {
            throw new ServiceException("新增校验失败:刷新模式不存在");
        }
        if (repository.count(ConfigBaseImpulseSenderHeaderQuery.builder().code(command.getCode()).build()) > 0) {
            throw new ServiceException("新增校验失败:配置已存在");
        }
        int index = 1;
        for (ConfigBaseImpulseSenderDetailCommand detailCommand : command.getDetailList()) {
            if (!Objects.equals(detailCommand.getSort(), index++)) {
                throw new ServiceException("新增校验失败:排序异常");
            }
            if (!EnumUtil.checkCode(detailCommand.getType(), ConfigBaseImpulseSenderDetailTypeEnum.values())) {
                throw new ServiceException("新增校验失败:类型不存在");
            }
            if (Objects.equals(detailCommand.getType(), ConfigBaseImpulseSenderDetailTypeEnum.CURRENT_DATE.getCode())
                    && (!EnumUtil.checkCode(Integer.parseInt(detailCommand.getValue()), ConfigBaseImpulseSenderDetailTypeDateFormatEnum.values()))) {
                throw new ServiceException("新增校验失败:时间格式不存在");
            }
            if (Objects.equals(detailCommand.getType(), ConfigBaseImpulseSenderDetailTypeEnum.SERIAL_NUMBER.getCode()) && (!StringUtil.equals(detailCommand.getValue(), "" + ConfigBaseImpulseSenderDetailTypeSerialModeEnum.NOT_FILLED.getCode()))) {
                if (!detailCommand.getValue().contains("-")) {
                    throw new ServiceException("新增校验失败:流水格式异常");
                }
                String modeCode = detailCommand.getValue().substring(0, detailCommand.getValue().indexOf("-"));
                String fillContent = detailCommand.getValue().substring(detailCommand.getValue().indexOf("-") + 1);
                if (!EnumUtil.checkCode(Integer.parseInt(modeCode), ConfigBaseImpulseSenderDetailTypeSerialModeEnum.values())) {
                    throw new ServiceException("新增校验失败:流水格式不存在");
                }
                if (StringUtil.isEmpty(fillContent)) {
                    throw new ServiceException("新增校验失败:流水填充字段不能为空");
                }
            }
        }
    }

    /**
     * 初始化发号器配置 业务校验
     *
     * @param command
     * @throws ServiceException
     */
    public void checkIntiCondition(ConfigDatImpulseSenderConditionCommand command) throws ServiceException {
        if (StringUtil.isEmpty(command.getCode())) {
            throw new ServiceException("初始化校验异常:未指定发号器");
        }
        if (repository.count(ConfigBaseImpulseSenderHeaderQuery.builder().code(command.getCode()).build()) <= 0) {
            throw new ServiceException("初始化校验异常:发号器不存在");
        }
        if (conditionRepository.count(ConfigDatImpulseSenderConditionQuery.builder().code(command.getCode()).build()) > 0) {
            throw new ServiceException("初始化校验异常:发号器被初始化");
        }
    }

}
